<template>
  <a-modal
    :open="open"
    width="780px"
    :title="rowData?.id ? '修改后送计划' : '新增后送计划'"
    centered
    :ok-button-props="{ disabled: !isCompleted }"
    ok-text="保存"
    @ok="onOk"
    @cancel="onCancel"
  >
    <a-form :model="formState" class="form-style-flex" :label-col="{ style: { width: '110px' } }">
      <a-form-item label="计划代号" class="w-1/2">
        <a-input v-model:value="formState.planCode" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="计划批号" class="w-1/2">
        <a-input v-model:value="formState.planNo" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="计划名称" class="w-1/2">
        <a-input v-model:value="formState.planName" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="换乘方式" class="w-1/2">
        <SelectItem
          v-model:value="formState.transferModeCode"
          type="DT00286"
          placeholder="请选择"
          @change="
            (record, option) => {
              formState.transferModeCode = record
              formState.transferModeName = option.label
            }
          "
        />
      </a-form-item>
      <a-form-item label="后送工具" class="w-1/2">
        <SelectItem
          v-model:value="formState.transferCode"
          type="DT00285"
          placeholder="请选择"
          @change="
            (record, option) => {
              formState.transferCode = record
              formState.transferName = option.label
            }
          "
        />
      </a-form-item>
      <a-form-item label="后送方向" class="w-1/2">
        <SelectItem
          v-model:value="formState.directionCode"
          type="DT00287"
          placeholder="请选择"
          @change="
            (record, option) => {
              formState.directionCode = record
              formState.directionName = option.label
            }
          "
        />
      </a-form-item>
      <a-form-item label="离开日期" class="w-1/2">
        <a-date-picker v-model:value="formState.leaveDate" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
      </a-form-item>
      <a-form-item label="到达日期" class="w-1/2">
        <a-date-picker v-model:value="formState.arrivalDate" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
      </a-form-item>
      <a-form-item label="换乘起始时间" class="w-1/2">
        <a-date-picker v-model:value="formState.transferStTime" show-time format="YYYY-MM-DD HH:mm:ss" value-format="YYYY-MM-DD HH:mm:ss" />
      </a-form-item>
      <a-form-item label="换乘结束时间" class="w-1/2">
        <a-date-picker v-model:value="formState.transferEdTime" show-time format="YYYY-MM-DD HH:mm:ss" value-format="YYYY-MM-DD HH:mm:ss" />
      </a-form-item>
      <a-form-item label="伤病员概要" class="w-full">
        <a-textarea v-model:value="formState.summary" :rows="3" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="其他" class="w-full">
        <a-textarea v-model:value="formState.otherDesc" :rows="3" placeholder="请输入" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script setup>
import { ref, watch } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'

const emit = defineEmits(['update:open', 'ok'])
const props = defineProps({
  open: {
    type: Boolean,
    default: false
  },
  rowData: {
    type: Object,
    default: () => ({})
  }
})
const formState = ref({
  planCode: '',
  planNo: '',
  planName: '',
  transferModeCode: '',
  transferModeName: '',
  transferCode: '',
  transferName: '',
  directionCode: '',
  directionName: '',
  leaveDate: '',
  arrivalDate: '',
  transferStTime: '',
  transferEdTime: '',
  summary: '',
  otherDesc: ''
})
const rulesState = ref({})
const { isCompleted, resetFields, validate } = useForm(formState, rulesState)

watch(
  () => props.open,
  (val) => {
    if (val && props.rowData?.id) {
      formState.value = props.rowData
    }
  }
)
const onCancel = () => {
  emit('update:open', false)
  resetFields()
  formState.value = {
    planCode: '',
    planNo: '',
    planName: '',
    transferModeCode: '',
    transferModeName: '',
    transferCode: '',
    transferName: '',
    directionCode: '',
    directionName: '',
    leaveDate: '',
    arrivalDate: '',
    transferStTime: '',
    transferEdTime: '',
    summary: '',
    otherDesc: ''
  }
}
const onOk = async () => {
  await validate()
  const { code, msg } = await post('/war/warEvacuationPlan/doSaveWarEvacuationPlan', formState.value)
  if (code === 0) {
    message.success('保存成功')
    onCancel()
    emit('ok')
  } else {
    message.error(msg)
  }
}
</script>
